package heap

import "hi-jvm/classfile"

/*
成员信息
 */
type ClassMember struct {
	AccessFlags uint16	// 访问标志
	Name        string
	Descriptor  string
	Class       *Class
}

func (self *ClassMember) copyMemberInfo(memberInfo *classfile.MemberInfo) {
	self.AccessFlags = memberInfo.AccessFlags
	self.Name = memberInfo.Name()
	self.Descriptor = memberInfo.Descriptor()
}


func (self *ClassMember) IsPublic() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_PUBLIC)
}

func (self *ClassMember) IsPrivate() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_PRIVATE)
}
func (self *ClassMember) IsProtected() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_PROTECTED)
}

func (self *ClassMember) IsStatic() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_STATIC)
}
func (self *ClassMember) IsFinal() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_FINAL)
}

// jvms 5.4.4
func (self *ClassMember) isAccessibleTo(d *Class) bool {
	if self.IsPublic() {
		return true
	}
	c := self.Class
	if self.IsProtected() {
		return d == c || d.isSubClassOf(c) ||
			c.getPackageName() == d.getPackageName()
	}
	if !self.IsPrivate() {
		return c.getPackageName() == d.getPackageName()
	}
	return d == c
}

